package com.example.jh352160.surfingtv.Index.customView.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.jh352160.surfingtv.R;
import com.example.jh352160.surfingtv.commodity_details.CommodityDetailsPage;

import java.util.List;

/**
 * Created by jh352160 on 2016/7/20.
 */

public class CommodityRecyclerViewAdapter extends RecyclerView.Adapter<CommodityRecyclerViewAdapter.ViewHolder> {

    private List<String> names, linetags, prices, imageUrls, fromList, detilUrlList;
    private List<Boolean> isLikeList;
    private List<Integer> targetId;
    private Context context;

    public CommodityRecyclerViewAdapter(Context context, List<String> imageUrls, List<String> names, List<String> linetags, List<String> prices, List<String> fromList, List<String> detilUrlList, List<Boolean> isLikeList, List<Integer> targetId) {
        // TODO: 2016/7/20 try to delete super() function
        super();
        this.names = names;
        this.linetags = linetags;
        this.prices = prices;
        this.fromList = fromList;
        this.detilUrlList = detilUrlList;
        this.isLikeList = isLikeList;
//        this.images=new int[]{R.drawable.commodity_goodman1,R.drawable.commodity_goodman2,R.drawable.commodity_goodman3};
        this.context = context;
        this.targetId = targetId;

        //this.imageUrls=new int[]{R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher};
//        this.names=new ArrayList<String>();
//        this.names.add("好先生1");
//        this.names.add("好先生2");
//        this.names.add("好先生3");
//        this.names.add("好先生3");
//        this.names.add("好先生3");
//
//        this.linetags=new ArrayList<String>();
//        this.linetags.add("宣传语1");
//        this.linetags.add("宣传语2");
//        this.linetags.add("宣传语3");
//        this.linetags.add("宣传语3");
//        this.linetags.add("宣传语3");
//
//        this.prices=new ArrayList<String>();
//        this.prices.add("￥100");
//        this.prices.add("￥200");
//        this.prices.add("￥300");
//        this.prices.add("￥300");
//        this.prices.add("￥300");

        this.imageUrls = imageUrls;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_index_commodity, null);
        final ViewHolder viewHolder = new ViewHolder(view);

        viewHolder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, CommodityDetailsPage.class);
                intent.putExtra("from", viewHolder.from);
                intent.putExtra("detilUrl", viewHolder.detilUrl);
                intent.putExtra("isLike", viewHolder.isLike);
                intent.putExtra("targetId", viewHolder.targetId);
                context.startActivity(intent);
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //holder.image.setImageResource(imageUrls.get(position));
        Glide.with(context).load(imageUrls.get(position)).into(holder.image);
        holder.name.setText(names.get(position));
        holder.linetag.setText(linetags.get(position));
        holder.price.setText(prices.get(position));
        holder.from = fromList.get(position);
        holder.detilUrl = detilUrlList.get(position);
        holder.isLike = isLikeList.get(position);
        holder.targetId = targetId.get(position);
    }

    @Override
    public int getItemCount() {
        return imageUrls.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView image;
        TextView name, linetag, price;
        String from, detilUrl;
        boolean isLike;
        int targetId;

        ViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.commodityImage);
            name = (TextView) itemView.findViewById(R.id.commodityName);
            linetag = (TextView) itemView.findViewById(R.id.commodityTagline);
            price = (TextView) itemView.findViewById(R.id.commodityPrice);
        }
    }
}
